package com.example.wishstar.viewmodel;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.example.wishstar.MyApplication;
import com.example.wishstar.model.NewHabit;
import com.example.wishstar.repository.AddHabitRepository;

import java.time.LocalDate;

public class AddHabitViewModel {
    private String TAG = "AddHabitViewModel";
    private static final String DEFAULT_FREQUENCY = "1111100";
    private ObservableField<NewHabit> newHabitObservableField;
    private Context context;
    private AddHabitRepository addHabitRepository;

    private int userId;
    private AddHabitCallBack addHabitCallBack;

    public interface AddHabitCallBack {
        void choiceFrequencyCallback(String frequency);
        void chocieHabitIconCallback(int postion);
    }

    public void setAddHabitCallBack(AddHabitCallBack addHabitCallBack) {
        this.addHabitCallBack = addHabitCallBack;
    }

    public AddHabitViewModel(Context context) {
        this.context = context;
        userId = MyApplication.userId;
        NewHabit newHabit = new NewHabit(userId, DEFAULT_FREQUENCY);

        newHabitObservableField = new ObservableField<>();
        newHabitObservableField.set(newHabit);
        addHabitRepository = new AddHabitRepository(MyApplication.getApi());
    }

    public String getHabitTitle() {
        return newHabitObservableField.get().habitTitle;
    }

    public void setHabitTitle(String habitTitle) {
        newHabitObservableField.get().habitTitle = habitTitle;
    }

    public void choiceFrequency1(View view) {
        choiceFrequency(0);
    }
    public void choiceFrequency2(View view) {
        choiceFrequency(1);
    }
    public void choiceFrequency3(View view) {
        choiceFrequency(2);
    }
    public void choiceFrequency4(View view) {
        choiceFrequency(3);
    }
    public void choiceFrequency5(View view) {
        choiceFrequency(4);
    }

    public void choiceFrequency6(View view) {
        choiceFrequency(5);
    }
    public void choiceFrequency7(View view) {
        choiceFrequency(6);
    }

    private void choiceFrequency(int postion) {
        String oldFrequency = newHabitObservableField.get().frequency;
        String newFrequency;
        if (oldFrequency.charAt(postion) == '0') {
            newFrequency = oldFrequency.substring(0, postion) + "1" + oldFrequency.substring(postion + 1);
            newHabitObservableField.get().setFrequency(newFrequency);
            Log.e(TAG, "choiceFrequency()" + newFrequency);
            addHabitCallBack.choiceFrequencyCallback(newFrequency);
        }
        else {
            newFrequency = oldFrequency.substring(0, postion) + "0" + oldFrequency.substring(postion + 1);
            newHabitObservableField.get().setFrequency(newFrequency);
            Log.e(TAG, "choiceFrequency()" + newFrequency);
            addHabitCallBack.choiceFrequencyCallback(newFrequency);
        }
    }



    public void choiceIcon1(View view) {
        newHabitObservableField.get().habitIcon = 1;
        addHabitCallBack.chocieHabitIconCallback(1);
    }

    public void choiceIcon2(View view) {
        newHabitObservableField.get().habitIcon = 2;
        addHabitCallBack.chocieHabitIconCallback(2);
    }
    public void choiceIcon3(View view) {
        newHabitObservableField.get().habitIcon = 3;
        addHabitCallBack.chocieHabitIconCallback(3);
    }
    public void choiceIcon4(View view) {
        newHabitObservableField.get().habitIcon = 4;
        addHabitCallBack.chocieHabitIconCallback(4);
    }
    public void choiceIcon5(View view) {
        newHabitObservableField.get().habitIcon = 5;
        addHabitCallBack.chocieHabitIconCallback(5);
    }

    public void choiceIcon6(View view) {
        newHabitObservableField.get().habitIcon = 6;
        addHabitCallBack.chocieHabitIconCallback(6);
    }

    public void choiceIcon7(View view) {
        newHabitObservableField.get().habitIcon = 7;
        addHabitCallBack.chocieHabitIconCallback(7);
    }
    public void choiceIcon8(View view) {
        newHabitObservableField.get().habitIcon = 8;
        addHabitCallBack.chocieHabitIconCallback(8);
    }
    public void choiceIcon9(View view) {
        newHabitObservableField.get().habitIcon = 9;
        addHabitCallBack.chocieHabitIconCallback(9);
    }
    public void choiceIcon10(View view) {
        newHabitObservableField.get().habitIcon = 10;
        addHabitCallBack.chocieHabitIconCallback(10);
    }

    public void choiceIcon11(View view) {
        newHabitObservableField.get().habitIcon = 11;
        addHabitCallBack.chocieHabitIconCallback(11);
    }

    public void choiceIcon12(View view) {
        newHabitObservableField.get().habitIcon = 12;
        addHabitCallBack.chocieHabitIconCallback(12);
    }
    public void choiceIcon13(View view) {
        newHabitObservableField.get().habitIcon = 13;
        addHabitCallBack.chocieHabitIconCallback(13);
    }
    public void choiceIcon14(View view) {
        newHabitObservableField.get().habitIcon = 14;
        addHabitCallBack.chocieHabitIconCallback(14);
    }
    public void choiceIcon15(View view) {
        newHabitObservableField.get().habitIcon = 15;
        addHabitCallBack.chocieHabitIconCallback(15);
    }
    public void choiceIcon16(View view) {
        newHabitObservableField.get().habitIcon = 16;
        addHabitCallBack.chocieHabitIconCallback(16);
    }

    public void post() {
        NewHabit newHabit = newHabitObservableField.get();
        Log.e(TAG, newHabit.toString());
        check();
        addHabitRepository.addHabit(newHabit);
    }

    public void check() {
        NewHabit newHabit = newHabitObservableField.get();
        int numOne = 0;
        for (int i = 0; i < newHabit.getFrequency().length(); ++i) {
            if (newHabit.getFrequency().charAt(i) == '1') {
                numOne++;
            }
        }
        if (numOne == 0) {
            Toast.makeText(context, "注意习惯频次每周至少有一天", Toast.LENGTH_SHORT).show();
        }
    }


}
